package com.loe.ui.quickjs

import android.webkit.JavascriptInterface
import com.loe.ui.UiSp
import com.quickjs.JSArray
import com.quickjs.JSContext
import com.quickjs.JSObject

fun JSContext.initSp()
{
    set("uiSp", JSObject(this).also()
    {
        appendJavascriptInterface(object
        {
            @JavascriptInterface
            fun getString(key: String, defValue: String): String
            {
                return UiSp.getString(key, defValue)
            }

            @JavascriptInterface
            fun putString(key: String, value: String)
            {
                UiSp.putString(key, value)
            }

            @JavascriptInterface
            fun getInt(key: String, defValue: Int): Int
            {
                return UiSp.getInt(key, defValue)
            }

            @JavascriptInterface
            fun putInt(key: String, value: Int)
            {
                UiSp.putInt(key, value)
            }

            @JavascriptInterface
            fun getDouble(key: String, defValue: Double): Double
            {
                return UiSp.getDouble(key, defValue)
            }

            @JavascriptInterface
            fun putDouble(key: String, value: Double)
            {
                UiSp.putDouble(key, value)
            }

            @JavascriptInterface
            fun getBoolean(key: String, defValue: Boolean): Boolean
            {
                return UiSp.getBoolean(key, defValue)
            }

            @JavascriptInterface
            fun putBoolean(key: String, value: Boolean)
            {
                UiSp.putBoolean(key, value)
            }

            @JavascriptInterface
            fun getObject(key: String): JSObject
            {
                return JSObject(context, UiSp.getJson(key))
            }

            @JavascriptInterface
            fun putObject(key: String, value: JSObject)
            {
                UiSp.putJson(key, value.toJSONObject())
            }

            @JavascriptInterface
            fun getArray(key: String): JSArray
            {
                return JSArray(context, UiSp.getJsonArray(key))
            }

            @JavascriptInterface
            fun putArray(key: String, value: JSArray)
            {
                UiSp.putJsonArray(key, value.toJSONArray())
            }

            @JavascriptInterface
            fun clear()
            {
                UiSp.clear()
            }
        })
    })
}